package com.example.chasshelper.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.chasshelper.R;
import com.example.chasshelper.SubjectQueryResultActivity;
import com.example.chasshelper.adapter.SubjectStuAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Course;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

public class SubjectStuFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private View view;
    private Activity mActivity;
    private Context mContext;
    //控件
    private EditText et_factor;
    private ImageView iv_toAdd;
    private ImageView iv_search;
    private RecyclerView rv_subject_stu;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private List<Course> courseList;
    private Student student;
    private ArrayList<Course> queryResult;
    //adapter
    private SubjectStuAdapter subjectStuAdapter;
    public SubjectStuFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static SubjectStuFragment newInstance(String param1, String param2) {
        SubjectStuFragment fragment = new SubjectStuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 绑定布局
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_subject_stu, container, false);
        }
        mActivity = getActivity();
        mContext = getContext();
        initUI();
        initData();
        return view;
    }

    private void initUI() {
        //绑定控件
        et_factor = view.findViewById(R.id.et_subjectId);
        iv_search = view.findViewById(R.id.iv_search);
        iv_toAdd = view.findViewById(R.id.iv_toAdd);
        rv_subject_stu = view.findViewById(R.id.rv_subject_stu);
        //点击事件
        iv_search.setOnClickListener(this);
        iv_toAdd.setOnClickListener(this);
    }
    private void initData() {
        //接收数据
        mActivity = getActivity();
        mContext = getContext();
        Bundle bundle = this.getArguments();
        student = (Student)bundle.getSerializable("student");
        //dao
//        courseList=courseDao.findAll();
        courseList=courseDao.findByStudentId(student);

        //adapter
        subjectStuAdapter = new SubjectStuAdapter(mContext,courseList,student);
        rv_subject_stu.setLayoutManager(new LinearLayoutManager(mContext));
        rv_subject_stu.setAdapter(subjectStuAdapter);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_search://当前页面搜索
                queryResult = (ArrayList<Course>) courseDao.findByIdOrCourseNameLike(et_factor.getText().toString());
                subjectStuAdapter.setList(queryResult);
                subjectStuAdapter.notifyDataSetChanged();
                break;
            case R.id.iv_toAdd://添加课程
                intent.putExtra("student", student);
                intent.setClass(mContext, SubjectQueryResultActivity.class);
                startActivity(intent);
                break;

        }
    }
}